<!-- 广告栏招商页 -->
<template>
	<view class="my-content attract">
		<view class="content">
			<buildForm v-if="formLoading" :value="form" :formItems="formItems" @input="formInput" />
			<view class="button">
			
			</view>
		</view>
		<u-button class="submit" hover-class="none" type="primary" @click="subForm">
			{{ $t('提交') }}
		</u-button>
		<view style="width: 100%;height:20rpx"></view>
		<vipPopup v-if="vipShow" :show="vipShow" @change="clickVipPopup" />
		<authNamePopup v-if="authShow" :show="authShow" @change="clickAuthPopup"></authNamePopup>
	</view>
</template>

<script>
	import buildForm from "@/components/buildForm/index.vue"
	import {
		mapForm
	} from 'pages/release/buildFormItems.js'
	import {
		attract
	} from './attract.js'
	import vipPopup from "@/components/vipPopup/index.vue";
	import authNamePopup from "@/components/authNamePopup/authNamePopup.vue";

	export default {
		data() {
			return {
				lang: getApp().globalData.lang,
				id: 0,
				form: {},
				formItems: [],
				formLoading: false, // 加载完成后再引入组件
				
				vipShow: false,
				//后台开关验证 vip权限
				vip:false,
				
				isAuthName: false,
				authShow: false,
			};
		},
		components: {
			buildForm,
			vipPopup,
			authNamePopup
		},
		onLoad(options) {
			this.formItems = attract
			mapForm(this.formItems, this.id).then(res => {
				this.form = res
				this.formLoading = true
				console.log(this.formItems);
			})
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: this.$t("广告栏招商")
			})
		},
		onShow() {
			this.getUserAuth() // 获取用户权限
			this.$u.api.user.appCheck().then(res => {
				this.report = res.report
				this.vip = res.vip
			})
			
			/* 验证是否实名认证 */
			this.$u.api.user.getUserAudit().then(res => {
				this.isAuthName = res.status == 2; //status !=2都是未实名认证的
			})
		},
		methods: {
			subForm() {
				const that = this
				if(this.lang == 'zh'){
					console.log(this.auth)
					if (this.auth.level > 0) {
						// if (this.auth.auth.indexOf("ad") === -1) { // 非会员不能提交
						// 	this.vipShow = true
						// 	// 弹出vip购买弹窗
						// 	return false
						// } else {
						// 	if (!this.isAuthName) {
						// 		this.authShow = true;
						// 		return;
						// 	}
						// }
					} else {
						
						if(that.vip){
							that.vipShow = true // 弹出vip购买弹窗
							return
						}else{
							uni.showModal({
								content: that.$t('您的权限不足哦!请联系工作人员!'),
								showCancel: false,
								confirmText: that.$t('是'),
							})
							return false;
						}
						
						return false
					}
				}
				
				uni.showLoading({
					title: "Loading...",
					mask: true
				})
				that.$u.api.user.createAdvertBusiness(that.form).then(res => {
					uni.hideLoading();
					this.$u.toast(this.$t('提交成功，请等待后台审核'))
					setTimeout(() => {
						that.$u.route({
							url: '/pages/tabbar/user/index',
							type: 'switchTab'
						})
					}, 3000)
				}).catch(err => {
					console.log(err)
				})
			},
			formInput(val) {
				this.form = val
			},
			getUserAuth() {
				const that = this
				that.$u.api.user.getUserAuth().then(res => {
					that.auth = res
				})
			},
			clickVipPopup() {
				this.vipShow = false
			},
			clickAuthPopup(e) {
				this.authShow = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: auto;
		background-color: #f6f6f6;
	}

	.attract {
		background: #f9f9f9;
		padding: 30rpx 30rpx 64rpx 30rpx;

		.content {
			width: 100%;
			height: auto;
			background: #ffffff;
			display: flex;
			flex-direction: column;
			margin-bottom: 80rpx;
		}
	}

	.button {
		flex: 1;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		padding-bottom: 46rpx;
	}
	.submit{
		width: 602rpx;
		height: 90rpx;
		border-radius: 16rpx;
		background: #000000;
	
	}
</style>